<template>
  <view>
    <!-- 昨日活跃奖励卡片 -->
    <view class="reward-card" v-if="showRewardCard && !rewardReceived">
      <view class="reward-card-content">
        <view class="reward-header">
          <FeatherIcon
            name="gift"
            size="24"
            color="#ff9f43"
            class="reward-icon"
          />
          <text class="reward-title">昨日活跃奖励</text>
        </view>
        <view class="reward-description">
          <text>恭喜您昨日活跃度达标，可领取奖励！</text>
        </view>
        <view class="reward-button-container">
          <view
            class="reward-button"
            @click="handleReceiveReward"
            :class="{ receiving: isReceiving }"
          >
            <text class="reward-button-text">{{
              isReceiving ? "领取中..." : "领取奖励"
            }}</text>
            <view class="reward-coins" v-if="isReceiving">
              <view class="coin" v-for="n in 8" :key="n"></view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 领取成功弹框 -->
    <view class="reward-success-modal" v-if="showRewardSuccess">
      <view class="modal-content">
        <view class="success-icon">
          <FeatherIcon name="check-circle" size="80" color="#4cd137" />
        </view>
        <view class="success-title">领取成功</view>
        <view class="success-amount">+{{ rewardAmount }} 积分</view>
        <view class="success-message">恭喜获得昨日活跃奖励</view>
        <view class="success-button" @click="closeRewardSuccess">太棒了</view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from "vue";
import FeatherIcon from "@/components/FeatherIcon.vue";
import { showError } from "@/utils/common.js";
import api from "@/api";

const props = defineProps({
  showRewardCard: {
    type: Boolean,
    default: false,
  },
  rewardReceived: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits(["update:showRewardCard", "update:rewardReceived"]);

const isReceiving = ref(false);
const showRewardSuccess = ref(false);
const rewardAmount = ref(0);

// 修改领取奖励的处理函数
const handleReceiveReward = async () => {
  if (props.rewardReceived || isReceiving.value) return;

  try {
    isReceiving.value = true;

    const res = await api.activity.collectYesterdayReward();

    // 延迟执行，让动画效果显示完
    setTimeout(() => {
      if (res) {
        if (res.sum === -1) {
          showError("今日已领取");
        } else {
          rewardAmount.value = res.sum;
          showRewardSuccess.value = true;
        }
        emit("update:rewardReceived", true);
      } else {
        showError(res.msg || "领取失败");
      }
      isReceiving.value = false;
    }, 1500);
  } catch (error) {
    showError(error.message || "领取失败");
    isReceiving.value = false;
  }
};

// 关闭奖励成功弹框
const closeRewardSuccess = () => {
  showRewardSuccess.value = false;
};
</script>

<style lang="scss">
/* 昨日活跃奖励卡片样式 */
.reward-card {
  margin: 24rpx;
  background: linear-gradient(135deg, #ffdf7e, #ffb142);
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 6rpx 16rpx rgba(255, 177, 66, 0.3);
  position: relative;

  .reward-card-content {
    padding: 30rpx;
    position: relative;
    z-index: 2;
  }

  &:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><circle cx="15" cy="15" r="5" fill="rgba(255,255,255,0.1)"/><circle cx="50" cy="50" r="8" fill="rgba(255,255,255,0.1)"/><circle cx="85" cy="85" r="5" fill="rgba(255,255,255,0.1)"/><circle cx="30" cy="70" r="6" fill="rgba(255,255,255,0.1)"/><circle cx="70" cy="30" r="6" fill="rgba(255,255,255,0.1)"/></svg>');
    background-repeat: repeat;
    background-size: 100rpx 100rpx;
    opacity: 0.4;
    z-index: 1;
  }

  .reward-header {
    display: flex;
    align-items: center;
    margin-bottom: 16rpx;

    .reward-icon {
      margin-right: 12rpx;
    }

    .reward-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #7d4614;
    }
  }

  .reward-description {
    color: #7d4614;
    font-size: 28rpx;
    margin-bottom: 24rpx;
  }

  .reward-button-container {
    display: flex;
    justify-content: center;
  }

  .reward-button {
    background-color: #ffffff;
    color: #ff9f43;
    font-size: 28rpx;
    font-weight: bold;
    padding: 16rpx 60rpx;
    border-radius: 40rpx;
    box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
    position: relative;
    overflow: hidden;
    transition: all 0.3s;
    animation: buttonPulse 2s infinite ease-in-out;

    &:active {
      transform: scale(0.96);
      box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);
    }

    &.receiving {
      padding: 16rpx 100rpx;
      background-color: #7d4614;
      color: #ffffff;
      animation: buttonPulse 1s infinite ease-in-out;
    }
  }

  .reward-button-text {
    position: relative;
    z-index: 2;
  }

  .reward-coins {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1;

    .coin {
      position: absolute;
      width: 16rpx;
      height: 16rpx;
      border-radius: 50%;
      background-color: #ffdf7e;
      animation: coinFly 1.5s ease-out;

      &:nth-child(1) {
        left: 20%;
        top: 50%;
        animation-delay: 0.1s;
      }
      &:nth-child(2) {
        left: 30%;
        top: 30%;
        animation-delay: 0.2s;
      }
      &:nth-child(3) {
        left: 40%;
        top: 70%;
        animation-delay: 0.3s;
      }
      &:nth-child(4) {
        left: 50%;
        top: 40%;
        animation-delay: 0.4s;
      }
      &:nth-child(5) {
        left: 60%;
        top: 60%;
        animation-delay: 0.5s;
      }
      &:nth-child(6) {
        left: 70%;
        top: 30%;
        animation-delay: 0.6s;
      }
      &:nth-child(7) {
        left: 80%;
        top: 70%;
        animation-delay: 0.7s;
      }
      &:nth-child(8) {
        left: 90%;
        top: 50%;
        animation-delay: 0.8s;
      }
    }
  }
}

@keyframes buttonPulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.15);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes coinFly {
  0% {
    transform: translate(0, 0) scale(1);
    opacity: 1;
  }
  100% {
    transform: translate(0, -100rpx) scale(0);
    opacity: 0;
  }
}

/* 奖励成功弹框样式 */
.reward-success-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;

  .modal-content {
    background: #fff;
    border-radius: 20rpx;
    padding: 40rpx;
    width: 80%;
    max-width: 600rpx;
    text-align: center;
    animation: modalPop 0.3s ease-out;

    .success-icon {
      margin-bottom: 20rpx;
      animation: successPop 0.5s ease-out;
    }

    .success-title {
      font-size: 36rpx;
      font-weight: bold;
      color: #333;
      margin-bottom: 20rpx;
    }

    .success-amount {
      font-size: 48rpx;
      color: #ff9f43;
      font-weight: bold;
      margin-bottom: 20rpx;
      animation: amountPop 0.5s ease-out 0.2s both;
    }

    .success-message {
      font-size: 28rpx;
      color: #666;
      margin-bottom: 40rpx;
    }

    .success-button {
      background: linear-gradient(135deg, #ff9f43, #ff7f50);
      color: #fff;
      padding: 20rpx 40rpx;
      border-radius: 40rpx;
      font-size: 32rpx;
      font-weight: bold;
      display: inline-block;
      box-shadow: 0 4rpx 12rpx rgba(255, 159, 67, 0.3);
      transition: all 0.3s;

      &:active {
        transform: scale(0.96);
        box-shadow: 0 2rpx 6rpx rgba(255, 159, 67, 0.3);
      }
    }
  }
}

@keyframes successPop {
  0% {
    transform: scale(0);
  }
  50% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes amountPop {
  0% {
    transform: translateY(20rpx);
    opacity: 0;
  }
  100% {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes modalPop {
  from {
    transform: scale(0.8);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}
</style>
